// intercept main view
TrvnInterceptorService.installInterceptor( {
	name : 'other.MainView',
	isAvailble : function() {
		return false;
	},
	intercept : function(doc) {
		doc.$G('#lleft').style.display = 'none';
		var obj = doc.evaluateXPath('//img[@alt="3X server"]')[0];
		if (obj) obj.style.display = 'none';
		doc.$G('#lmidlc').style.minWidth = '552px';
		doc.$G('#lres0').style.left = '-30px'
		doc.$G('#ltop5').style.left = '101px';
	}
});
// intercept message select all
TrvnInterceptorService.installInterceptor( {
	name : 'other.MessageSelectAll',
	isAvailble : function() {
		return true;
	},
	intercept : function(doc) {
		if (!doc.URL.contains('berichte.php') && !doc.URL.contains('nachrichten.php')) return;
		var fn = function(checked) {
			var cbs = doc.evaluateXPath(TrvnCfg.queryXPath('messageCheckboxes'));
			cbs.each(function(cb) {
				cb.checked = checked;
			});
		}
		fn(true);
		if (TrvnCfg.getCurrentAccount().plus) return;
		var sa = doc.createElement('input');
		sa.type = 'checkbox';
		sa.title = '全选';
		sa.checked = true;
		sa.addEventListener('click', function() {
			fn(sa.checked);
		}, false);
		var delButton = doc.evaluateXPath(TrvnCfg.queryXPath('messageDeleteButton'))[0];
		if (delButton) {
			delButton.parentNode.insertBefore(sa, delButton);
		}
	}
});
// intercept select village
TrvnInterceptorService.installInterceptor( {
	name : 'other.SelectVillage',
	isAvailble : function() {
		return true;
	},
	intercept : function(doc) {
		var inputVlg = doc.evaluateXPath('//input[@name="dname"]')[0];
		if (!inputVlg) return;
		var vlgTable = doc.evaluateXPath(TrvnCfg.queryXPath('villageTable'))[0];
		if (vlgTable) {
			var rows = vlgTable.tBodies[0].rows;
			for (var i = 0;i < rows.length; i++) {
				var td = rows[i].cells[1];
				td.style.cursor = 'pointer';
				td.addEventListener('mouseover', function() {
					this.style.color = '#71D000';
				}, false);
				td.addEventListener('mouseout', function() {
					this.style.color = '';
				}, false);
				td.addEventListener('click', function() {
					inputVlg.value = this.parentNode.cells[0].innerHTML.stripTags().substr(8)
				}, false);
			}
		}
		var selectVlg = doc.$C('select');
		var vlgs = TrvnCfg.getCurrentAccount().villages;
		var size = 1;
		selectVlg.innerHTML = '<option value="">-</option>';
		TrvnUtils.each(vlgs, function(villageName, village) {
			var option = doc.$C('option');
			option.innerHTML = village.title;
			option.value = village.title;
			selectVlg.appendChild(option);
			size++;
		});
		selectVlg.addEventListener('change', function() {
			inputVlg.value = this.value;
		}, false);
		selectVlg.style.position = 'absolute';
		selectVlg.size = size > 10 ? 10 : size;
		inputVlg.parentNode.appendChild(selectVlg);

	}
});
// intercept resource
TrvnInterceptorService.installInterceptor( {
	name : 'other.Resource',
	isAvailble : function() {
		return true;
	},
	intercept : function(doc) {
		if (!doc.URL.contains('build.php')) return;
		var span = doc.$G('lmid2').lastChild;
		if (span.tagName == 'FORM') span = span.lastChild.previousSibling.lastChild;
		else if (span.tagName == 'IMG') span = span.previousSibling.lastChild;
		else return;
		if (span.tagName != 'SPAN' || !span.innerHTML.contains('足')) return;
		var table = span.previousSibling;
		while (table.tagName != 'TABLE') {
			table = table.previousSibling;
			if (!table) return;
		}
		var rs = table.rows[0].innerHTML.stripTags().split('|');
		var r = {
			lumber : TrvnUtils.parseNumber(rs[0].trim()),
			clay : TrvnUtils.parseNumber(rs[1].trim()),
			iron : TrvnUtils.parseNumber(rs[2].trim()),
			crop : TrvnUtils.parseNumber(rs[3].trim())
		};
		var vlg = TrvnCfg.getCurrentVillage();
		if (r.lumber > vlg.capacity.warehouse || r.clay > vlg.capacity.warehouse || r.iron > vlg.capacity.warehouse) {
			span.innerHTML = '提示：请升级仓库';
			return;
		}
		if (r.crop > vlg.capacity.granary) {
			span.innerHTML = '提示：请升级粮仓';
			return;
		}
		var q = TrvnResourceManager.getCurrentQuantity(vlg);
		var dis = {}, t = {}, maxT = 0;
		with (Math) {
			dis.lumber = max(r.lumber - q.lumber, 0);
			dis.clay = max(r.clay - q.clay, 0);
			dis.iron = max(r.iron - q.iron, 0);
			dis.crop = max(r.crop - q.crop, 0);
			t.lumber = parseInt(dis.lumber * (3600000 / vlg.productivity.lumber));
			t.clay = parseInt(dis.clay * (3600000 / vlg.productivity.clay));
			t.iron = parseInt(dis.iron * (3600000 / vlg.productivity.iron));
			t.crop = parseInt(dis.crop * (3600000 / vlg.productivity.crop));
			maxT = max(t.lumber, max(t.clay, max(t.iron, t.crop)));
		}
		var date = new Date();
		date.setTime(date.getTime() + maxT);
		span.innerHTML = '提示：资源大约于 ' + date.toLocaleString() + ' 充足<br />' + '<img class="res" src="'
				+ TrvnCfg.queryUrl('img/un/r/1.gif') + '" />' + dis.lumber + '&nbsp;|&nbsp;<img class="res" src="'
				+ TrvnCfg.queryUrl('img/un/r/2.gif') + '" />' + dis.clay + '&nbsp;|&nbsp;<img class="res" src="'
				+ TrvnCfg.queryUrl('img/un/r/3.gif') + '" />' + dis.iron + '&nbsp;|&nbsp;<img class="res" src="'
				+ TrvnCfg.queryUrl('img/un/r/4.gif') + '" />' + dis.crop;
	}
});
// intercept sort village table
TrvnInterceptorService.installInterceptor( {
	name : 'other.SortVillageTable',
	isAvailble : function() {
		return true;
	},
	intercept : function(doc) {
		var vlgTable = doc.evaluateXPath(TrvnCfg.queryXPath('villageTable'))[0];
		if (!vlgTable) return;
		var thead = vlgTable.createTHead();
		thead.innerHTML = '<tr><td class="center">村庄</td><td class="center">坐标</td><td class="center">类型</td></tr>';
		var vlg = TrvnCfg.getCurrentVillage();
		var l = vlg.location.split('|');
		var curVlgLocation = [TrvnUtils.parseNumber(l[0]), TrvnUtils.parseNumber(l[1])];
		new SortedTable(vlgTable, {
			getColumnComparator : function(colIndex) {
				return {
					compare : function(o1, o2) {
						if (colIndex == 1) {
							var l1 = Math.pow(curVlgLocation[0] - o1[0], 2) + Math.pow(curVlgLocation[1] - o1[1], 2);
							var l2 = Math.pow(curVlgLocation[0] - o2[0], 2) + Math.pow(curVlgLocation[1] - o2[1], 2);
							return l2 - l1;
						}
						if (o1 < o2) return 1;
						if (o2 < o1) return -1;
						return 0;
					}
					,
				};
			},
			getSortableValueAt : function(rowObj, cellObj) {
				var value = cellObj.innerHTML;
				if (cellObj.cellIndex == 1) {
					value = value.stripTags().replace(/\(|\)/g, '').split('|');
					return [TrvnUtils.parseNumber(value[0]), TrvnUtils.parseNumber(value[1])];
				}
				if (cellObj.cellIndex == 0) {
					return value.stripTags();
				}
				return value;
			}
		});
	}
});
// intercept attack form
TrvnInterceptorService.installInterceptor( {
	name : 'other.AttackForm',
	isAvailble : function() {
		return true;
	},
	intercept : function(doc) {
		if (!doc.URL.contains('a2b.php')) return;
		var form = doc.evaluateXPath('//form[@name="snd"]')[0];
		if (!form) return;
		form.target = '_blank';
	}
});